import { getallevent, upDateComment, addevent, delItem } from '../../../api/getData.js';
import { getTimeStm } from '../../../utils/util.js';
const globalData = getApp().globalData
const app = getApp()


Component({
  properties: {
  },

  data: {
    table: '0',
    eventList: [],
    page: 1,
    total: 0,
    show: false,
    calendar: null,
    startTime: "",
    endTime: "",
    showPicker: false,
    timeSign: '',
    nowTime: '',
    inputValue: '',
    isShow: false,//选择descriList的picker
    descriList: ['停机保养', '设备清洁', '设备维修', '其他'],
    choosedIndex: 0, // 被选中的描述
    scrollHight: '100%'
  },
  ready(){
    this.getEvent()
  },
  methods: {
    // 操作相关
    selectTitle(e){
      let table = e.target.dataset.commentable;
      if (this.data.table === table) return;
      this.setData({
        table,
        eventList: [],
        page: 1
      })
      this.getEvent()
    },
    // 父页面获取事件列表
    getEventByPa(page = this.data.page) {
      getallevent(this.data.table, page)
        .then(res => {
          if (res.state === "SUCCESS") {
            console.log(res.result.list)
            this.setData({
              eventList: res.result.list,
              total: res.result.total
            })
          }
        })
    },
    // 获取异常事件
    getEvent(page = this.data.page){
      getallevent(this.data.table, page)
        .then( res =>{
          if (res.resCode === "SUCCESS"){
            console.log(res.result.list)
            let eventList = this.data.eventList.concat(...res.result.list)
            this.setData({
              eventList: eventList,
              total: res.result.total
            })
          }
        })
    },
    // 编辑更新
    upDataDesc( e ){
      let item = e.currentTarget.dataset.item;
      let value = e.detail.value;
      if (item.comment === value) return
      upDateComment(item.startTime, item.endTime, value)
    },

    // 添加事件dialog
    addEventDialog (e) {
      const { descriList, choosedIndex } = this.data;
      this.setData({
        show: true,
        startTime: "",
        endTime: "",
        inputValue: descriList[choosedIndex]
      })
      this.triggerEvent('changeShow', {
        showDialog: 'show'
      })
    },

    // 隐藏
    hide(){
      this.setData({
        show: false
      })
      this.triggerEvent('changeShow', {
        showDialog: ''
      })
    },
    // 是否展示描述下拉框
    showDialogfn() {
      this.setData({
        isShow: this.data.isShow === false ? true : false
      })
    },
    // 获取input值
    getInp(e){
      this.setData({
        inputValue: e.detail.value
      })
    },

    handleSelecteDate(e) {
      let { date, timeSign } = e.detail,
        timeArr = [getTimeStm(this.data.startTime), getTimeStm(this.data.endTime)],
        dataStm = new Date().getTime();
      timeArr[timeSign] = getTimeStm(date);
      if (timeArr[timeSign] > dataStm){
        wx.showToast({
          title: '时间不能大于当前时间',
          icon: 'none'
        })
        return
      }
      if (timeArr[0] === timeArr[1]) {
        wx.showToast({
          title: '时间不能相等',
          icon: 'none'
        })
        return
      }

      if (timeArr[0] > timeArr[1]) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: 'none'
        })
        return
      }

      if (timeArr[1] - timeArr[0] > 15724800000) {
        wx.showToast({
          title: '时间不能大于半年',
          icon: 'none'
        })
        return
      }

      let obj = {};
      obj[timeSign == 0 && "startTime" || "endTime"] = date;
      this.setData(obj)
      this.hiddencover()
    },
    pickerShow: function (e) {
      let sign = e.currentTarget.dataset.sign;
      this.setData({
        showPicker: true,
        chartHide: true,
        timeSign: e.currentTarget.dataset.sign
      });
      this.selectComponent('.date-picker').formMatInit(sign == 0 && this.data.startTime || this.data.endTime)

    },
    // 点击确定
    sure(){
      let { startTime, endTime, inputValue} = this.data;

      let that = this
      if (!startTime || !endTime || !inputValue){
        let title = !startTime && '开始时间必填' || (!endTime && '恢复时间必填' || '描述必填');
        wx.showToast({
          title,
          icon: 'none'
        })
        return
      }
      let tmsStart = new Date(startTime.split('-').join('/')).getTime(),
        tmsEnd = new Date(endTime.split('-').join('/')).getTime(),
        nowTms = new Date().getTime();
      if (tmsStart > nowTms || tmsEnd > nowTms){
        wx.showToast({
          title: '时间不能大于当前时间',
          icon: 'none'
        })
        return
      }
      addevent(tmsStart, tmsEnd, inputValue)
        .then( res =>{
          if (res.resCode === "SUCCESS"){
            wx.showToast({
              title: '添加成功！',
              icon: 'none'
            })
            this.setData({
              eventList: [],
              page: 1,
              choosedIndex: 0
            })

            setTimeout(() => {
              that.triggerEvent('changeShow', {
                showDialog: ''
              })
              that.getEvent(1)
              that.setData({
                show: false
              })
            }, 800)
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none'
            })
          }
          
        })
    },
    hiddencover() {
      this.setData({
        showPicker: false,
        chartHide: false
      })
    },

    // 页面上拉加载更多
    reachBottom(){
      let eventLength = this.data.eventList.length;
      if (eventLength >= this.data.total) {
        wx.showToast({
          title: '已经到底了',
          icon: 'none'
        })
        return
      }
      this.setData({
        page: ++this.data.page
      })
      this.getEvent()
    },


    bindPickerChange: function (e) {
      this.getData(this.data.sensorList[e.detail.value].id);
      this.setData({
        index: e.detail.value,
        sensorId: this.data.sensorList[e.detail.value].id
      });
    },
    setPickerTime: function (val) {
      let data = val.detail;
      this.setData({
        startTime: data.startTime,
        endTime: data.endTime
      });
    },
    // 阻止蒙版层滚动
    preventTouchMove(e) {
      return false
    },
    // 选择描述
    chooseDescribe(e) {
      this.setData({
        choosedIndex: e.detail.value[0]
      })
      if (this.data.descriList.length - 1 === this.data.choosedIndex) { // 其他
        this.setData({
          inputValue: ''
        })
      }
    },
    // 确定选择
    pickerSure () {
      if (this.data.descriList.length - 1 !== this.data.choosedIndex) { // 没有选择其他
        this.setData({
          inputValue: this.data.descriList[this.data.choosedIndex]
        })
      }
      this.showDialogfn()
    },
    pickerCancel (){
      this.showDialogfn()
    },
    // 删除
    delItem (e) {
      wx.showModal({
        title: '提示',
        content: '确定要删除本条事件吗？',
        success:  (res) => {
          if (res.confirm) {
            delItem(e.currentTarget.dataset.id).then(res => {
              if (res.resCode === "SUCCESS") {
                let that = this;
                wx.showToast({
                  title: res.message,
                  icon: 'none'
                })
                this.setData({
                  eventList: [],
                  page: 1
                })
                setTimeout(() => {
                  that.triggerEvent('changeShow', {
                    showDialog: ''
                  })
                  that.getEvent(1)
                }, 50)
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'none'
                })
              }
            })
          } else {
            // 取消删除
          }
        }
      })
    }
  }
})